package com.time.company.components.popup;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.time.company.R;
import com.time.company.base.BasePW;
import com.time.company.utils.MyDateUtils;
import com.time.company.utils.TimeUtils;
import com.time.company.utils.ToastUtils;
import com.time.company.views.wheel.OnWheelScrollListener;
import com.time.company.views.wheel.WheelView;
import com.time.company.views.wheel.adapter.NumericWheelAdapter;
import java.util.Calendar;

public class DatePW extends BasePW {

    public static final int START_TIME = 20;
    public static final int END_TIME = 21;

    private Context context;
    private String initTime, currentTime;
    private static String startTime;
    private int curYear, curMonth;

    private LayoutInflater mInflater;
    private View view;
    private TextView titleTv;
    private TextView cancelTv;
    private TextView confirmTv;

    private WheelView yearView;
    private WheelView monthView;
    private WheelView dayView;
    private WheelView hourView;
    private WheelView minView;
    private int[] timeInt;

    public String resultDate;
    public int dateType = -1;

    public DatePW(Context context, String initTime, int type) {
        this.context = context;
        this.initTime = initTime;
        this.dateType = type;
        this.currentTime = MyDateUtils.getCurrentTime();
        this.resultDate = MyDateUtils.getCurrentTime();
        initTime();
        initWindow();
    }

    private void initTime() {
        timeInt = new int[6];
        timeInt[0] = Integer.valueOf(initTime.substring(0, 4));
        timeInt[1] = Integer.valueOf(initTime.substring(4, 6));
        timeInt[2] = Integer.valueOf(initTime.substring(6, 8));
        timeInt[3] = Integer.valueOf(initTime.substring(8, 10));
        timeInt[4] = Integer.valueOf(initTime.substring(10, 12));
//        timeInt[5] = Integer.valueOf(initTime.substring(12, 14));
    }

    private void initWindow() {
        mInflater = LayoutInflater.from(context);
        view = mInflater.inflate(R.layout.pw_wheel_view, null);
        titleTv = this.view.findViewById(R.id.tv_wheel_title);
        cancelTv = this.view.findViewById(R.id.tv_cancel);
        confirmTv = this.view.findViewById(R.id.tv_confirm);
        yearView = this.view.findViewById(R.id.wheel_year);
        monthView = this.view.findViewById(R.id.wheel_month);
        dayView = this.view.findViewById(R.id.wheel_day);
        hourView = this.view.findViewById(R.id.wheel_hour);
        minView = this.view.findViewById(R.id.wheel_minute);
        initWheel();
    }

    private void initWheel() {
        setContentView(view);

        Calendar calendar = Calendar.getInstance();
        curYear = calendar.get(Calendar.YEAR);
        curMonth = calendar.get(Calendar.MONTH) + 1;

        NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(context, curYear, curYear + 10);
        numericWheelAdapter1.setLabel("");
        yearView.setViewAdapter(numericWheelAdapter1);
        yearView.setCyclic(true);
        yearView.addScrollingListener(scrollListener);

        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(context, 1, 12, "%02d");
        numericWheelAdapter2.setLabel("月");
        monthView.setViewAdapter(numericWheelAdapter2);
        monthView.setCyclic(true);
        monthView.addScrollingListener(scrollListener);

        NumericWheelAdapter numericWheelAdapter3 = new NumericWheelAdapter(context, 1,
                getDay(curYear, curMonth), "%02d");
        numericWheelAdapter3.setLabel("日");
        dayView.setViewAdapter(numericWheelAdapter3);
        dayView.setCyclic(true);
        dayView.addScrollingListener(scrollListener);

        NumericWheelAdapter numericWheelAdapter4 = new NumericWheelAdapter(context, 0, 23, "%02d");
        numericWheelAdapter4.setLabel("");
        hourView.setViewAdapter(numericWheelAdapter4);
        hourView.setCyclic(true);
        hourView.addScrollingListener(scrollListener);

        NumericWheelAdapter numericWheelAdapter5 = new NumericWheelAdapter(context, 0, 59, "%02d");
        numericWheelAdapter5.setLabel("");
        minView.setViewAdapter(numericWheelAdapter5);
        minView.setCyclic(true);
        minView.addScrollingListener(scrollListener);

        yearView.setCurrentItem(timeInt[0] - curYear);
        monthView.setCurrentItem(timeInt[1] - 1);
        dayView.setCurrentItem(timeInt[2] - 1);
        hourView.setCurrentItem(timeInt[3]);
        minView.setCurrentItem(timeInt[4]);
        yearView.setVisibleItems(5);//设置显示行数
        monthView.setVisibleItems(5);
        dayView.setVisibleItems(5);
        hourView.setVisibleItems(5);
        minView.setVisibleItems(5);
        setWidth(LayoutParams.MATCH_PARENT);
        setHeight(LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(false);
        setFocusable(true);
        setAnimationStyle(R.style.Pw_Bottom_Animation);
        ColorDrawable dw = new ColorDrawable(0x80000000);
        setBackgroundDrawable(dw);
        this.titleTv.setText(dateType == START_TIME ? "选择开始时间" : "选择结束时间");
        this.cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        this.confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    if (dateType == START_TIME){
                        // 提示开始时间小于当前时间

                        if (TimeUtils.compareTime(resultDate, currentTime)){
                            ToastUtils.showShort("开始时间不能小于当前时间");
                            return;
                        }else {
                            startTime = resultDate;
                        }
                    } else {
                        // 提示结束时间小于当前时间或者开始时间
                        if (TimeUtils.compareTime(resultDate, currentTime)){
                            ToastUtils.showShort("结束时间不能小于当前时间");
                            return;
                        }

                        ToastUtils.showShort("Start time " + startTime);
                        if (TimeUtils.compareTime(resultDate, startTime)){
                            ToastUtils.showShort("结束时间不能小于开始时间");
                            return;
                        }
                    }
                    onItemClickListener.onItemClick(dateType, resultDate);
                }
                dismiss();
            }
        });
    }

    private int getDay(int year, int month) {
        int day;
        boolean flag;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {

        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int n_year = yearView.getCurrentItem() + curYear;//年
            int n_month = monthView.getCurrentItem() + 1;//月
            initDay(n_year, n_month);
            resultDate = new StringBuilder().append((yearView.getCurrentItem() + curYear))
                    .append("-").append((monthView.getCurrentItem() + 1) < 10 ? "0" +
                            (monthView.getCurrentItem() + 1) : (monthView.getCurrentItem() + 1))
                    .append("-").append(((dayView.getCurrentItem() + 1) < 10) ? "0" +
                            (dayView.getCurrentItem() + 1) : (dayView.getCurrentItem() + 1))
                    .append(" ").append(((hourView.getCurrentItem()) < 10) ? "0" +
                            (hourView.getCurrentItem()) : (hourView.getCurrentItem()))
                    .append(":").append(((minView.getCurrentItem()) < 10) ? "0" +
                            (minView.getCurrentItem()) : (minView.getCurrentItem()))
                    .append(":00")
                    .toString();
        }
    };

    private void initDay(int year, int month) {
        NumericWheelAdapter numericWheelAdapter = new
                NumericWheelAdapter(context, 1, getDay(year, month), "%02d");
        numericWheelAdapter.setLabel("日");
        dayView.setViewAdapter(numericWheelAdapter);
    }

}
